package the.workshop.bu.service.impl;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.wisteria.framework.web.QueryResult;
import org.wisteria.framework.web.mvc.dto.ConditionDTO;
import the.broccoli.setting.service.IDictService;
import the.workshop.bu.dto.CreditDTO;
import the.workshop.bu.dto.CreditCondition;
import the.workshop.bu.dto.CreditProductCondition;
import the.workshop.bu.dto.CreditVO;
import the.workshop.bu.entity.Credit;
import the.workshop.bu.entity.CreditProduct;
import the.workshop.bu.mapper.CreditMapper;
import the.workshop.bu.service.ICreditProductService;
import the.workshop.bu.service.ICreditService;
import org.wisteria.framework.web.mvc.service.AbstractSuperService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 申贷 服务实现类
 * </p>
 *
 * @author lei.liu
 * @since 2020-06-15
 */
@Service
public class CreditServiceImpl extends AbstractSuperService<CreditMapper, Credit> implements ICreditService {

    @Resource
    private IDictService dictService;

    @Resource
    private ICreditProductService creditProductService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(CreditDTO dto) {
        Credit credit = dto.toCredit();
        baseMapper.insert(credit);

        List<CreditProduct> creditProductList = dto.getProducts(credit.getId());
        if (!CollectionUtils.isEmpty(creditProductList)) {
            creditProductService.saveBatch(creditProductList);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateById(CreditDTO dto) {
        super.updateById(dto.toCredit());

        creditProductService.deleteByCreditId(dto.getId(), dto.getModifiedId(), dto.getModifiedName());
        if (!CollectionUtils.isEmpty(dto.getProducts(dto.getId()))) {
            creditProductService.saveBatch(dto.getProducts(dto.getId()));
        }
    }

    @Override
    public CreditVO detailById(Integer id) {
        CreditCondition condition = new CreditCondition();
        condition.setId(id);
        List<CreditVO> list = listByCondition(condition);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        CreditVO vo = list.get(0);

        CreditProductCondition cpConditon = new CreditProductCondition();
        cpConditon.setCreditId(id);
        vo.setProducts(creditProductService.listByCondition(cpConditon));

        return vo;
    }

    @Override
    public QueryResult pageList(ConditionDTO condition, Integer pageNum, Integer pageSize) {
        QueryResult<CreditVO> queryResult = super.pageList(condition, pageNum, pageSize);
        if (!CollectionUtils.isEmpty(queryResult.getItems())) {
            for (CreditVO dto : queryResult.getItems()) {
                dto.setStatusName(dictService.getName(dto.getStatus()));
            }
        }
        return queryResult;
    }


}
